package com.chinacache.maint.webssh.service.ssh;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.orm.hibernate.SimpleHibernateDao;
import com.chinacache.maint.webssh.entity.ssh.Device;

@Transactional
public class DeviceManager {
	private SimpleHibernateDao<Device, String> deviceDao;

	@Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		deviceDao = new SimpleHibernateDao<Device, String>(sessionFactory,
				Device.class);
	}

	@Transactional(readOnly = true)
	public Device getDevice(String id) {
		return deviceDao.findById(id);
	}

	@Transactional(readOnly = true)
	public Page<Device> getAllDevice(Page<Device> page) {
		return deviceDao.findAll(page);
	}

	public Page<Device> getAllDeviceByCondition(Page<Device> page,
			String condition) {

		if (condition != null && condition.length() > 0) {
			condition = condition.trim();
			String queryString = "select device from Device device where device.devId like "
					+ "'%"
					+ condition
					+ "%'"
					+ " or device.adminIp like "
					+ "'%"
					+ condition
					+ "%'"
					+ " or device.devName like "
					+ "'%"
					+ condition.toUpperCase()
					+ "%'"
					+ " or device.devLabel like "
					+ "'%"
					+ condition.toUpperCase() + "%'";
			return deviceDao.find(page, queryString);
		} else {
			return deviceDao.findAll(page);
		}
	}
}
